#include "videothread.h"
#include <QProcess>
#include <QCoreApplication>
#include <QDir>
#include "easytool.h"

//#define NOT_RELOAD
VideoThread::VideoThread()
{

}

void VideoThread::run()
{
    emit videoStatusSignals(-1,"Video Thread Start");
    qDebug() << "Video文件路径路径：" << binPath;


    QString AppPath = QCoreApplication::applicationDirPath();
    QString gltempPath = AppPath + "/gltemp";

#ifndef NOT_RELOAD
    EasyTool::isDirExist(gltempPath);
    EasyTool::clearDir(gltempPath);
    EasyTool::isDirExist(gltempPath+"/image");

    process.start("cmd");
    process.waitForStarted();

    /////////////////////////////////////////////////////////////
    emit videoStatusSignals(-1,"开始转换黑白视频......");
    args.clear();
    args.append("-i");
    args.append(binPath);
    args.append("-ss");
    args.append("00:00:00");
    args.append("-t");
    args.append("00:01:30");
    args.append("-r");
    args.append("20");
    args.append("-vf");
    args.append("scale=128:64");
    args.append(gltempPath+"/output.mp4");
    process.execute(AppPath + "/ffmpeg/ffmpeg.exe",args);

    emit videoStatusSignals(-1,"转换黑白视频完成");
    /////////////////////////////////////////////////////////////

    emit videoStatusSignals(-1,"开始提取帧图......");

    args.clear();
    args.append("-i");
    args.append(gltempPath+"/output.mp4");
    args.append("-r");
    args.append("20");
    args.append("-vf");
    args.append("hue=s=0");
    args.append(gltempPath+"/image/%d.png");
    process.execute(AppPath + "/ffmpeg/ffmpeg.exe",args);

    emit videoStatusSignals(-1,"提取帧图完成");
    /////////////////////////////////////////////////////////////

    emit videoStatusSignals(-1,"开始提取音频......");

    args.clear();
    args.append("-i");
    args.append(gltempPath+"/output.mp4");
    args.append("-vn");
    args.append("-f");
    args.append("wav");
    args.append("-ar");
    args.append("8192");
    args.append("-ac");
    args.append("1");
    args.append(gltempPath+"/audio.wav");
    process.execute(AppPath + "/ffmpeg/ffmpeg.exe",args);

    emit videoStatusSignals(-1,"提取音频完成");
#endif
    glfile.creat(gltempPath);
    emit videoStatusSignals(-3,gltempPath);

exit:
    emit videoStatusSignals(-2,"Video Thread Exit");
}

void VideoThread::init(void)
{

}

void VideoThread::setPath(QString path)
{
    binPath = path;

}
